<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    function double(num) {
      return num * 2
    }

    function pow(num) {
      return num ** 2
    }

    function composeFn(...fns) {
      
      var length = fns.length
      if (length <= 0) {
        return
      }
      for (let i = 0; i < length; i++) {
        if (typeof fns[i] !== "function" ) {
          throw new Error(`index postion ${i} must be function`)
        }        
      }
      
      return function(...args) {
        var result = fns[0].apply(this, args)
        for (let i = 0; i < length; i++) {
          result = fns[i].apply(this,[result])          
        }
      }
    }

    var newFn = composeFn(double, pow, console.log)
    newFn(100)
    newFn(50)
    newFn(22)
  </script>
</body>
</html>